//
//  MDNetworkUtils.m
//  MideaWorld_Zizhu
//
//  Created by Minus on 4/23/15.
//  Copyright (c) 2015 Minus. All rights reserved.
//

#import "MDNetworkUtils.h"
#import "AFNetworking.h"
#import "BaseAPI.h"

#define TestURL @""

@interface MDNetworkUtils ()

@end

@implementation MDNetworkUtils

+ (instancetype)getInstance
{
    static MDNetworkUtils *_sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:BaseAPI_get_server_url_()]];
//        _sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:TestURL]];
        _sharedInstance.requestSerializer.timeoutInterval = 25.0;
    });
    
    return _sharedInstance;
}

- (void)post:(NSString *)url parameters:(id)params completion:(void (^)(NSString *, NSError *))completion
{
    NSLog(@"post 参数: %@", params);
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [self POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData *tempData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:NULL];
        NSString *tempString = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
        tempString = [tempString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        
        NSLog(@"request url: %@", [operation.request.URL absoluteString]);
        NSLog(@"response data: %@", tempString);
        
        if (completion)
        {
            completion(tempString, nil);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"request url: %@", [operation.request.URL absoluteString]);
        NSLog(@"request error: %@", [error localizedDescription]);
        
        if (completion)
        {
            completion(nil, error);
        }
    }];
}

- (void)upload:(NSString *)url parameters:(id)params image:(UIImage *)image completion:(void (^)(NSString *, NSError *))completion
{
    NSLog(@"post 参数: %@", params);
    
    NSString *extension = @"png";
    NSString *mimeType = @"image/png";
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSData *pngData = UIImagePNGRepresentation(image);
    
    if (!pngData)
    {
        pngData = UIImageJPEGRepresentation(image, 1.0);
        extension = @"jpeg";
        mimeType = @"image/jpeg";
    }
    
    if ([pngData length] > (200*1024))
    {
        pngData = UIImageJPEGRepresentation(image, 0.5);
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *fileName = [dateFormatter stringFromDate:[NSDate date]];
    fileName = [fileName stringByAppendingPathExtension:extension];
    
    NSLog(@"image type: %@", extension);
    
    [self POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:pngData name:@"file" fileName:fileName mimeType:mimeType];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData *tempData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:NULL];
        NSString *tempString = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
        tempString = [tempString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        
        NSLog(@"request url: %@", [operation.request.URL absoluteString]);
        NSLog(@"response data: %@", tempString);
        
        if (completion)
        {
            completion(tempString, nil);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"request url: %@", [operation.request.URL absoluteString]);
        NSLog(@"request error: %@", [error localizedDescription]);
        
        if (completion)
        {
            completion(nil, error);
        }
    }];
}

@end
